package com.example.landlord.demo.Controller;

import com.example.landlord.demo.Common.Result;
import com.example.landlord.demo.Controller.request.ForgetRequest;
import com.example.landlord.demo.Controller.request.LoginRequest;
import com.example.landlord.demo.Controller.request.RegisterRequest;
import com.example.landlord.demo.Entity.User;
import com.example.landlord.demo.Service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService iUserService;
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest request){
        return Result.success(iUserService.login(request));
    }
    @GetMapping("/list")
    public Result list(){
        List<User> list= iUserService.list();
        return Result.success(list);
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        User user = iUserService.getById(id);
        return Result.success(user);
    }
    @PutMapping("/updateP")
    public Result updateP(@RequestBody User user) {
        iUserService.updateP(user);
        return Result.success();
    }
    //根据用户名查找picture
    @GetMapping("/a/{uname}")
    public Result getByUname(@PathVariable String uname) {
        User user = iUserService.getByuname(uname);
        System.out.println(user.toString());
        return Result.success(user);
    }
    @PostMapping("/register")
    public Result register(@RequestBody RegisterRequest request){
        iUserService.register(request);
        return Result.success();
    }

    @PostMapping("/forget")
    public Result forget(@RequestBody ForgetRequest request){
        iUserService.forget(request);
        return Result.success();
    }

    @PostMapping("/change")
    public Result forget(@RequestBody User request){
        iUserService.change(request);
        return Result.success();
    }

    }


